home *** CD-ROM | disk | FTP | other *** search
/ Sound Fx / Sound Fx.iso / Software / UNZIPED / MPW181-5 / _SETUP.1 / bit_res.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-04-19  |  1.6 KB  |  95 lines

  1. /* bit_res.cpp
  2.  
  3.     Implementation of Bit Reservoir for Layer III
  4.  
  5.    Adapted from the public c code by Jeff Tsay. */
  6.  
  7.  
  8. #ifdef  __WIN32__
  9. #define STRICT
  10. #include <windows.h>
  11. #endif   // __WIN32__
  12.  
  13. #include "all.h"
  14. #include "bit_res.h"
  15.  
  16. Bit_Reserve::Bit_Reserve()
  17. {
  18.     uint32 shifted_one = 1;
  19.  
  20.     offset       = 0;
  21.    totbit         = 0;
  22.    buf_byte_idx = 0;
  23.     buf          = new uint32[BUFSIZE];
  24.     buf_bit_idx  = 8;
  25.     putmask      = new uint32[32];
  26.  
  27.    putmask[0] = 0;
  28.  
  29.    for (int32 i=1; i<32; i++)
  30.    {
  31.        putmask[i] = putmask[i-1] + shifted_one;
  32.       shifted_one <<= 1;
  33.    }
  34. }
  35.  
  36. Bit_Reserve::~Bit_Reserve ()
  37. {
  38.     delete [] putmask;
  39.     delete [] buf;
  40. }
  41.  
  42. uint32 Bit_Reserve::hgetbits(uint32 N)
  43. // read N bits from the bit stream
  44. {
  45.  totbit += N;
  46.  
  47.  uint32 val = 0;
  48.  int32 j = N;
  49.  
  50.  while (j > 0) {
  51.  
  52.     if (buf_bit_idx == 0) {
  53.          buf_bit_idx = 8;
  54.          buf_byte_idx++;
  55. /*    if (buf_byte_idx > offset)
  56.       { printf("Buffer overflow !!\n");exit(3); } */
  57.     }
  58.  
  59.     int32 k = (j < buf_bit_idx) ? j : buf_bit_idx;
  60.  
  61.    // BUFSIZE = 4096 = 2^12, so
  62.    // buf_byte_idx%BUFSIZE == buf_byte_idx & 0xfff
  63.    int32 tmp = buf[buf_byte_idx & 0xfff] & putmask[buf_bit_idx];
  64.    buf_bit_idx -= k;
  65.     tmp = tmp >> buf_bit_idx;
  66.    j -=k;
  67.    val |= tmp << j;
  68.  }
  69.  return(val);
  70. }
  71.  
  72.  
  73. void Bit_Reserve::hputbuf(int32 val)
  74. // write 8 bits into the bit stream
  75. {
  76.   buf[offset & 0xfff] = val;
  77.   offset++;
  78. }
  79.  
  80. void Bit_Reserve::rewindNbits(int32 N)
  81. {
  82.     totbit -= N;
  83.     buf_bit_idx += N;
  84.     while(buf_bit_idx >= 8)
  85.     {  buf_bit_idx -= 8;
  86.         buf_byte_idx--;
  87.     }
  88. }
  89.  
  90. void Bit_Reserve::rewindNbytes(int32 N)
  91. {
  92.    totbit -= (N << 3);
  93.     buf_byte_idx -= N;
  94. }
  95.